﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QLBH.DATA
{
    public class QuyenNhanVienData
    {
        private static QLBHEntities db = new QLBHEntities();
        public string Error { set; get; }

        public QUYEN_NHAN_VIEN LayQuyenTheoMaND(string ma_nguoi_dung, int ma_chuc_nang)
        {
            return db.QUYEN_NHAN_VIEN.Where(p => p.MA_NGUOI_DUNG == ma_nguoi_dung && p.MA_CHUC_NANG == ma_chuc_nang).FirstOrDefault();
        }

        public bool CapNhatQuyen(string ma_nguoi_dung, int ma_chuc_nang, int so_quyen)
        {
            try
            {
                var quyen = db.QUYEN_NHAN_VIEN.Where(p => p.MA_NGUOI_DUNG == ma_nguoi_dung && p.MA_CHUC_NANG == ma_chuc_nang).FirstOrDefault();
                if (quyen == null)
                {
                    quyen = new QUYEN_NHAN_VIEN();
                    quyen.MA_CHUC_NANG = ma_chuc_nang;
                    quyen.SO_QUYEN = so_quyen;
                    quyen.MA_NGUOI_DUNG = ma_nguoi_dung;
                    db.QUYEN_NHAN_VIEN.Add(quyen);
                    db.SaveChanges();
                    return true;
                }
                else
                {
                    quyen.SO_QUYEN = so_quyen;
                    db.SaveChanges();
                    return true;
                }
            }
            catch( Exception ex)
            {
                Error = ex.Message + "--" + ex.InnerException.Message;
                return false;
            }

        }
    }
}